package com.yangfan.springcloud.controller;

import com.yangfan.springcloud.entities.CommonResult;
import com.yangfan.springcloud.entities.Payment;
import com.yangfan.springcloud.service.IMessageProvider;
import com.yangfan.springcloud.service.PaymentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/payment")
@RefreshScope//配置中心 动态刷新的注解 加在业务类上面
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping(value = "/create")
    public CommonResult<Payment> create (@RequestBody Payment payment){
        int i = paymentService.create(payment);
        if(i > 0){
            return new CommonResult<>(200,"插入成功"+port);
        }
        return new CommonResult<>(444,"插入失败");
    }


    @GetMapping(value = "/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable(value = "id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        return new CommonResult(200,"查询成功"+port,payment);
    }


    @GetMapping(value = "/timeout")
    public CommonResult timeout()
    {

        try {
            TimeUnit.SECONDS.sleep(3L);
            return new CommonResult(444,"超时了！！！8002");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }



    @Value("${config.info}")
    private String version;

    /**
     * 测试 分布式配置中心 配合消息总线 实现 云端修改 消息总线 通知订阅
     * 下面只是把  修改的 内容打印出来
     * @return
     */
    @GetMapping(value = "/getInfo")
    public CommonResult getInfo()
    {
        return new CommonResult(200,version + "端口:" + port);
    }

    @Resource
    private IMessageProvider iMessageProvider;

    /**
     * 生产者 生产消息 往 消息中间件 发送消息 的方法
     * @return
     */
    @GetMapping(value = "/message")
    public CommonResult message()
    {
        iMessageProvider.send();
        return new CommonResult(200,"发送成功" + port);
    }


}
